/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.xuexiang.xuidemo.utils;

import android.content.Context;

import com.xuexiang.xuidemo.entity.Decision;
import com.xuexiang.xutil.XUtil;
import com.xuexiang.xutil.data.BaseSPUtil;

import java.util.ArrayList;
import java.util.List;

public class ZhuanpanSPUtils extends BaseSPUtil {

    private static volatile ZhuanpanSPUtils zInstance = null;

    public ZhuanpanSPUtils(Context context, String spName) {
        super(context, spName);
    }

    public ZhuanpanSPUtils(Context context) {
        super(context);
    }

    /**
     * 获取单例
     *
     * @return
     */
    public static ZhuanpanSPUtils getInstance() {
        if (zInstance == null) {
            synchronized (ZhuanpanSPUtils.class) {
                if (zInstance == null) {
                    zInstance = new ZhuanpanSPUtils(XUtil.getContext());
                }
            }
        }
        return zInstance;
    }

    public int getChoice() {
        return getInt("CHOICE",0);
    }

    public void setChoice(int choice) {
        putInt("CHOICE",choice);
    }

    public int getSum() {
        return getInt("SUM",0);
    }

    public Decision getDecisionById(int id) {
        if(getInt("NUM" + id,0)==0) return null;
        int num = getInt("NUM" + id,0);
        String title = getString("TITLE"+id,"");
        List<String> results = new ArrayList<>();
        List<Integer> weights = new ArrayList<>();
        for(int i=0;i<num;i++) {
            if(!contains("RESULTS" + id+"NUM"+i)) continue;
            results.add(getString("RESULTS" + id+"NUM"+i,""));
            weights.add(getInt("WEIGHTS" + id+"NUM"+i,0));
        }
        return new Decision(num,id,title,results,weights);
    }

    public List<Decision> getAllDecision() {
        int sum = getSum();
        List<Decision> decisions = new ArrayList<>();
        for(int i=0;i<sum;i++)
        {
            if(contains("NUM"+i)) {
                decisions.add(getDecisionById(i));
            }
        }
        return decisions;
    }

    public void addDecision(Decision decision) {
        int sum = getSum();
        putInt("NUM" + sum,decision.getNum());
        putString("TITLE" + sum,decision.getTitle());
        List<String> results = decision.getResults();
        List<Integer> weights = decision.getWeights();
        for(int i=0;i<decision.getNum();i++) {
            putString("RESULTS" + sum + "NUM" + i,results.get(i));
            putInt("WEIGHTS" + sum+"NUM"+i,weights.get(i));
        }
        putInt("SUM",sum+1);
    }

    public void deleteDecision(int id) {
        int num = getInt("NUM" + id,0);
        remove("NUM" + id);
        remove("TITLE"+id);
        for(int i=0;i<num;i++) {
            remove("RESULTS" + id+"NUM"+i);
            remove("WEIGHTS" + id+"NUM"+i);
        }
    }

    public void updateDecision(Decision decision) {
        int id = decision.getId();
        putInt("NUM" + id,decision.getNum());
        putString("TITLE" + id,decision.getTitle());
        List<String> results = decision.getResults();
        List<Integer> weights = decision.getWeights();
        for(int i=0;i<decision.getNum();i++) {
            putString("RESULTS" + id + "NUM" + i,results.get(i));
            putInt("WEIGHTS" + id+"NUM"+i,weights.get(i));
        }
    }

    public int getLastColorStyle() {
        return getInt("LASTCOLORSTYLE",-1);
    }
    public void setLastColorStyle(int lastColorStyle) {
        putInt("LASTCOLORSTYLE",lastColorStyle);
    }
}
